package com.ruoyi.system.interAttr.attr;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.interAttr.domain.GamesRes;
import com.ruoyi.system.interAttr.domain.InterAttrRes;
import com.ruoyi.system.interAttr.domain.InterServerRes;
import com.ruoyi.system.model.domain.SalePartner;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * 破晓属性匹配
 * @author ruoyi
 * @data 2024/7/5
 * @apiNote
 */
@Service
public class KAIGEAttr implements AdapterAttr {
    @Override
    public List<GamesRes> getGames(SalePartner salePartner) {
        List<GamesRes> resultList = new ArrayList<>();
        String allGame = getAllGame(salePartner);
        List<JSONObject> array = JSONObject.parseArray(JSONObject.parseObject(allGame).getString("data"), JSONObject.class);
        for (JSONObject jsonObject : array) {
            GamesRes res = new GamesRes();
            res.setId(jsonObject.getString("game_id"));
            res.setGameName(jsonObject.getString("game_id_name"));
            resultList.add(res);
        }
        return resultList;
    }

    @Override
    public List<InterServerRes> getServer(String gameId, SalePartner salePartner) {

        return new ArrayList<>();
    }

    @Override
    public List<InterAttrRes> getPropety(String gameId, String type, SalePartner salePartner) {
        List<InterAttrRes> resultList = new ArrayList<>();

        String atta = getAtta(gameId,salePartner);
        List<JSONObject> array = JSONObject.parseArray(JSONObject.parseObject(atta).getString("data"), JSONObject.class);
        if (!StringUtils.equals("3", type)) {
            for (JSONObject json : array) {
                if (StringUtils.equals(json.getString("title"),"游戏区服")){
                    continue;
                }
                InterAttrRes res = new InterAttrRes();
                res.setId(json.getString("id"));
                res.setName(json.getString("title"));
                List<JSONObject> children = JSONObject.parseArray(json.getString("children"), JSONObject.class);
                List<InterAttrRes> list = new ArrayList<>();
                for (JSONObject child : children) {
                    InterAttrRes res1 = new InterAttrRes();
                    res1.setId(child.getString("id"));
                    res1.setName(child.getString("title"));
                    list.add(res1);
                }
                res.setInterServerRes(list);
                resultList.add(res);
            }
        }

        return resultList;
    }

    public static void main(String[] args) {
//        getAtta("10");
//        getAllGame();
//        getAllServer("11");
    }

    static String getAllServer(String gameId) {
        String url = "http://mhh.kaigewangyou.com/api/api.php?pg=getAllServer";

        Map<String, Object> params = new TreeMap<>();
        params.put("pg", "getAllServer");
        params.put("gameId", gameId);
        String str = getCover1(params) + "key=" + "x8logf11d6o898l83botgg105c9ky83rjoi";
        String sign = DigestUtils.md5Hex(str).toUpperCase();
        params.put("signKey", sign);
        String response = HttpUtils.doPostForm(url, params, null);
        System.out.println(JSONObject.parseObject(response));
        return response;
    }

    static String getAtta(String gameId, SalePartner salePartner) {
        String url = "http://mhh.kaigewangyou.com/api/api.php?pg=getGameId";

        Map<String, Object> params = new TreeMap<>();
        params.put("pg", "getGameId");
        params.put("gameId", gameId);
        String str = getCover1(params) + "key=" + salePartner.getSaleKey();
        String sign = DigestUtils.md5Hex(str).toUpperCase();
        params.put("signKey", sign);
        String response = HttpUtils.doPostForm(url, params, null);
        System.out.println(JSONObject.parseObject(response));
        return response;
    }

    static String getAllGame(SalePartner salePartner) {
        String url = "http://mhh.kaigewangyou.com/api/api.php?pg=getAllGame";

        Map<String, Object> params = new TreeMap<>();
        params.put("pg", "getAllGame");
        String str = getCover1(params) + "key=" + salePartner.getSaleKey();
        String sign = DigestUtils.md5Hex(str).toUpperCase();
        params.put("signKey", sign);
        String response = HttpUtils.doPostForm(url, params, null);
        System.out.println(JSONObject.parseObject(response));
        return response;
    }

    public static String getCover1(Map<String, Object> params) {
        StringBuffer resut = new StringBuffer();
        for (String key : params.keySet()) {
            resut.append(key + "=" + params.get(key) + "&");
        }
        return resut.toString();
    }
}
